package com.example.controller;

import com.example.pojo.Result;
import com.example.pojo.changeModelPojo;
import com.example.service.changeModelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * ClassName:ChangModelController
 * Description:
 *
 * @Author 赵永峰
 * @Create 2024/7/17 14:37
 * @Version 1.0
 */
@RestController
@Slf4j
public class ChangModelController {
    @Autowired
    private changeModelService changeModelService;
    //插入一行新的数据
    @PostMapping("/insertChangeModel")
    public Result insertChangeModel(@RequestBody changeModelPojo changModel){
        changeModelService.insertOne(changModel);
        return Result.success();
    }
    //依据用户输入的作业指导书类型，进行查询
    @PostMapping("/inquiryType")
    public Result inquiryType(@RequestBody String type){
        List<changeModelPojo> changeModelList=changeModelService.inquiryByOneField("bookType",type);
        return Result.success(changeModelList);
    }
    //修改一行数据，修改的是表头数据
    @PostMapping("/editChangeModelTable")
    public Result editChangeModelTable(@RequestBody changeModelPojo changeModel){
        changeModelService.editChangeModelTable(changeModel);
        return Result.success();
    }
    //修改一个int类型的字段，现在用在修改标记位上
    @PostMapping("/editChangeModelFieldInt")
    public Result editChangeModelFieldInt(@RequestBody Map<String, Object> parameters){
        changeModelService.editFieldInt(parameters);
        return Result.success();
    }
    //修改一个字符串类型的字段，现在用在修改作业指导书上
    @PostMapping("/editChangeModelField")
    public Result editChangeModelField(@RequestBody Map<String, Object> parameters){
        String filed=changeModelService.editChangeModelField(parameters);
        return Result.success(filed);
    }
    //上传图片的函数(新页面上线后可删除)
    @PostMapping("/editChangeModelPicture")
    public Result editChangeModelPicture(@RequestParam("file") MultipartFile file, @RequestParam Integer id, @RequestParam String fieldName, @RequestParam String fieldVal){
        //上传照片的接口
        String allName = changeModelService.editChangeModelPicture(file,id,fieldName,fieldVal);
        return Result.success(allName);
    }

    //读取文件，返回给前端，用作保存数据用
    @GetMapping("/changeModelDownload")
    public Result changeModelDownload(@RequestParam("fileName") String fileName){
        byte[] file= changeModelService.readFile(fileName);
        return Result.success(file);
    }
    //删除文件,用在图片上
    @PostMapping("/changeModelPictureRemove")
    public Result changeModelPictureRemove(@RequestBody Map<String, Object> parameters) {
        String fileName=changeModelService.oilPictureRemove(parameters);
        return Result.success(fileName);
    }
    /*新页面上用到的函数*/
    //上传图片的函数（新页面用）
    @PostMapping("/editChangeModelPictureNew")
    public Result editChangeModelPictureNew(@RequestParam("file") MultipartFile file, @RequestParam Integer id, @RequestParam String fieldName, @RequestParam String fieldVal,@RequestParam String fileName){
        String allName = changeModelService.editChangeModelPictureNew(file,id,fieldName,fieldVal,fileName);
        return Result.success(fieldVal);
    }
    //删除文件用的函数
    @PostMapping("/changeModeNewPictureRemove")
    public Result changeModeNewPictureRemove(@RequestBody Map<String, Object> parameters) {
        String fileName=changeModelService.PictureRemove(parameters);
        return Result.success(fileName);
    }
}
